'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')
const define = require('./src/utils/define.js')
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin')
const webpack = require('webpack')
const CopyWebpackPlugin = require("copy-webpack-plugin");

function resolve(dir) {
  return path.join(__dirname, dir)
}

const name = defaultSettings.title || '干部管理综合业务平台' // page title

// If your port is set to 80,
// use administrator privileges to execute the command line.
// For example, Mac: sudo npm run
// You can change the port by the following method:
// port = 3000 npm run dev OR npm run dev --port = 3000
const port = process.env.port || process.env.npm_config_port || 3000 // dev port

// All configuration item explanations can be find in https://cli.vuejs.org/config/
module.exports = {
  /**
   * You will need to set publicPath if you plan to deploy your site under a sub path,
   * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
   * then publicPath should be set to "/bar/".
   * In most cases please use '/' !!!
   * Detail: https://cli.vuejs.org/config/#publicpath
   */
  publicPath: '/',
  outputDir: 'dist',
  assetsDir: 'static',
  // lintOnSave: process.env.NODE_ENV === 'development',
  lintOnSave: false,
  productionSourceMap: false,
  devServer: {
    port: port,
    open: false,
    overlay: {
      warnings: false,
      errors: true
    },
    // before: require('./mock/mock-server.js'),
    // 接口转发
    proxy: {
      '/dev': {
        target: define.APIURl,
        changeOrigin: true,
        pathRewrite: {
          '^/dev': ''
        }
      },
      '/api/bendi': {
        // target: 'http://192.168.1.63:2009', //process.env.VUE_APP_USERCENTER_BASE_API, // 设置你调用的接口域名和端口号 别忘了加http, //要访问的跨域的域名,只是域名
        // target: 'http://192.168.101.148:2009', //process.env.VUE_APP_USERCENTER_BASE_API, // 设置你调用的接口域名和端口号 别忘了加http, //要访问的跨域的域名,只是域名
        target: 'http://192.168.1.78:2006', //宪镇  process.env.VUE_APP_USERCENTER_BASE_API, // 设置你调用的接口域名和端口号 别忘了加http, //要访问的跨域的域名,只是域名
        // target: 'http://192.168.1.107:2009', //process.env.VUE_APP_USERCENTER_BASE_API, // 设置你调用的接口域名和端口号 别忘了加http, //要访问的跨域的域名,只是域名
        changeOrigin: true, // proxy的changeOrigin是false：请求头中host仍然是浏览器发送过来的host；如果设置成true：发送请求头中host会设置成target。
        pathRewrite: {
          '^/api/bendi': ''
        }
      },
      '/sdpc-user': {
        target: 'http://192.168.1.16:9091/api/service-user', //process.env.VUE_APP_USERCENTER_BASE_API, // 设置你调用的接口域名和端口号 别忘了加http, //要访问的跨域的域名,只是域名
        changeOrigin: true, // proxy的changeOrigin是false：请求头中host仍然是浏览器发送过来的host；如果设置成true：发送请求头中host会设置成target。
        pathRewrite: {
          '^/sdpc-user': '/api/service-user'
        }
      },
      '/knowledgeUrl': {
        target: `http://192.168.1.75:9666`,
        changeOrigin: true,
        pathRewrite: { '^/knowledgeUrl': `` },
      },
    }
  },
  // css: {
  //   loaderOptions: {
  //     // 设置 scss 公用变量文件
  //     sass: {
  //       data: `@import '~@/api/gz/assets/style/public.scss';`
  //     }
  //   }
  // },
  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: name,
    resolve: {
      alias: {
        '@': resolve('src'),
        'static': resolve('static') // 增加这一行代码
      }
    },
    plugins: [
      new MonacoWebpackPlugin({
        // available options are documented at https://github.com/Microsoft/monaco-editor-webpack-plugin#options
        languages: ['javascript', 'css', 'html', 'typescript', 'json', 'java', 'sql']
      }),
      new CopyWebpackPlugin([{
        from:
          './node_modules/@foxitsoftware/foxit-pdf-sdk-for-web-library/lib/',
        to: "foxit-lib",
        force: true,
        ignore: [
          "{PDFViewCtrl,UIExtension}.js",
          "{PDFViewCtrl,UIExtension}.{vendor,polyfills}.js",
          "preload-jr-worker.js"
        ]
      }])
    ]
  },
  chainWebpack(config) {
    config.externals({
      // 'monaco-editor': 'monaco-editor',
      'echarts': 'echarts'
    })
    config.plugins.delete('preload') // TODO: need test
    config.plugins.delete('prefetch') // TODO: need test
    config.plugin("provide").use(webpack.ProvidePlugin, [
      {
        "window.Quill": "quill"
      }
    ]);

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    // set preserveWhitespace
    config.module
      .rule('vue')
      .use('vue-loader')
      .loader('vue-loader')
      .tap(options => {
        options.compilerOptions.preserveWhitespace = true
        return options
      })
      .end()

    config
      // https://webpack.js.org/configuration/devtool/#development
      .when(process.env.NODE_ENV === 'development',
        config => config.devtool('cheap-source-map')
      )
    config
      .entry("preload")
      .add(path.resolve("src/preload.js"))
      .end()
      .plugin("html")
      .tap(args => {
        const [options,] = args;
        options.chunks = ["preload", "chunk-vendors", "app"];
        options.chunksSortMode = 'manual';
        return args;
      })
      .end()
      .module.rule("js")
      .exclude.add(/node_modules|license\-key\.js/);
    config.module
      .rule("eslint")
      .exclude.add(/node_modules|license\-key\.js/);
    config.module
      .rule('addon')
      .type('javascript/auto')
      .test(/\addon\.info\.json$/)
      .use('babel')
      .loader('babel-loader')
      .options({
        "presets": ["@babel/env"]
      })
      .end()
      .use('addon-loader')
      .loader('svg-sprite-loader')
      .end()
    config.externals(['UIExtension', 'PDFViewCtrl'])
  }
}
